package com.user.control;

import java.util.List;
import java.util.Random;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.user.Myhidernate.HibernateSessionFactory;
import com.user.domain.LoginInfo;

public class createToken {
	private String getRandomString(int length){
	     String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
	     Random random=new Random();
	     StringBuffer sb=new StringBuffer();
	     for(int i=0;i<length;i++){
	       int number=random.nextInt(62);
	       sb.append(str.charAt(number));
	     }
	     return sb.toString();
	 }
	public String getString(){
		String str = getRandomString(11);
		SessionFactory sf = HibernateSessionFactory.getSessionFactory();
		Session s =sf.getCurrentSession();
		s.beginTransaction();
		@SuppressWarnings("unchecked")
		List<LoginInfo> strs = s.createQuery("from LoginInfo where token='"+str+"'").list();
		s.getTransaction().commit();
		if(strs.size()==0){
			return str;
		}else{
			getString();
		}
		return str;
	}
}
